################################################
# Prepare POPPA Data
################################################

cat("\nPreparing POPPA data.")

library(tidyverse)
library(haven)

############################################
# POPPA Data and GALTAN Estimation
############################################

poppa <- read_stata("POPPA/party_means.dta")

# Add Party Family Labels

poppa$partyfamily_poppa <- factor(poppa$family, 
                          levels = 1:11,
                          label = c("Radical Right",
                                    "Conservatives",
                                    "Liberal",
                                    "Christian Democratic",
                                    "Social Democratic",
                                    "Radical Left",
                                    "Green",
                                    "Regionalist",
                                    "No Family",
                                    "Confessional",
                                    "Agrarian/Center"))

# Rename Party and Country Names

poppa$partyname_poppa <- poppa$party %>% paste
poppa$country_poppa <- poppa$country_id %>% as_factor %>% paste

# Positions

poppa$lrecon_poppa <- poppa$lrecon
poppa$populism_poppa <- poppa$populism
names(poppa)[9:13] <- paste0(names(poppa)[9:13], "_poppa")

# IDs

poppa$id_parlgov <- poppa$parlgov_id %>% as.numeric()
poppa$id_ches <- poppa$ches_id %>% as.numeric()
poppa$id_poppa <- poppa$party_id %>% as.numeric()
poppa$id_cmp <- poppa$cmp_id %>% as.numeric()

# GALTAN based on POPPA Variables (factor analysis)

galtan_df <- select(poppa,
                    id_poppa,
                    immigration,
                    nativism, 
                    laworder,
                    lifestyle) %>% 
  na.omit()

factor_analysis <- factanal(select(galtan_df,
                                   -id_poppa), 
                            factors = 1, 
                            rotation = "varimax", 
                            scores = "regression")

# Table A7

factor_analysis$loadings
factor_analysis$uniqueness

galtan_df$galtan_poppa <- as.numeric(factor_analysis$scores)

names(galtan_df)[2:5] <- paste0(names(galtan_df[2:5]), "_poppa")

poppa <- left_join(poppa,
                   galtan_df,
                   by = "id_poppa")

poppa_prep <- select(poppa,
                     ends_with("_poppa", ignore.case = F),
                     starts_with("id_", ignore.case = F)) %>%
  select(country_poppa,
         partyname_poppa,
         starts_with("id_"),
         everything())

saveRDS(poppa_prep, "POPPA/poppa_prep.rds")

cat("\nPOPPA data preparation finished.\n")